package com.example.controller;

import com.example.entity.Role;
import com.example.entity.Student;
import com.example.service.StudentService;
import com.example.service.TeacherService;
import com.example.service.UserService;
import com.example.utils.PageUtil;
import com.example.utils.RestResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * @author： wzz
 * @date： 2021-10-19 14:58
 */

@Slf4j
@Controller
public class StudentController {

    @Resource
    private StudentService studentService;
    @Resource
    private UserService userService;
    @Resource
    TeacherService teacherService;
    /**
     * 查询所有学生信息
     */
    @RequestMapping(value = "/back/queryStu")
    @ResponseBody
    public RestResponses queryStu(@RequestParam(value = "num",required = false,defaultValue = "")String num ,
                                  Integer page, Integer limit) {
        List<Student> studentList = null;
        if(!num.equals("")){
           studentList = studentService.queryMy(num);
        }else{
           studentList = studentService.queryAll();
        }
        if (null != studentList) {
            HashMap hashMap = PageUtil.PageByList(studentList, page, limit, studentList.size());
            return RestResponses.ok(hashMap);
        } else {
            return RestResponses.fail("查询出错");
        }
    }

    @GetMapping("/back/queryMy")
    public String queryMy(Authentication authentication,Integer page,Integer limit){
        return "redirect:/back/queryStu?num="+authentication.getName()+"&page="+page+"&limit="+limit;
    }

    /**
     * 查询所有班级
     *
     * @return
     */
    @RequestMapping(value = "/back/stuManager")
    public String queryclasses(Model model) {
        List<String> list = studentService.queryClasses();
        if (null != list) {
            model.addAttribute("classes", list);
        } else {
            list.add(0, "暂无班级");
            model.addAttribute("classes", list);
        }
        return "back/UserManage/stuManager";
    }

    @GetMapping("/back/myStudent")
    public String myStudent(Model model,Authentication authentication){
        List<String> list = studentService.queryClasses();
        if (null != list) {
            model.addAttribute("classes", list);
        } else {
            list.add(0, "暂无班级");
            model.addAttribute("classes", list);
        }
        List<String> bookList = teacherService.getMyBooks(authentication.getName());
        if (null != bookList) {
            model.addAttribute("bookList", bookList);
        } else {
            bookList.add(0, "暂无班级");
            model.addAttribute("bookList",  bookList);
        }
        return "back/UserManage/MyStudents";
    }

    /**
     * 根据条件查找符合要求的学生信息
     *
     * @param studentNumber
     * @param name
     * @param classes
     * @return
     */
    @RequestMapping(value = "/back/conditionSearch")
    @ResponseBody
    public RestResponses conditionSearch(@RequestParam(value = "studentNumber",required = false) String studentNumber,
                                         @RequestParam(value = "name",required = false) String name,
                                         @RequestParam(value = "classes",required = false) String classes
                                       ) throws IOException {

        log.info("studentNumber：{}", studentNumber);
        log.info("name：{}", name);
        log.info("classes：{}", classes);
        if ("请选择标签".equals(classes)) {
            classes = "";
        }
        if (studentNumber == null) {
            System.out.println("yes");
        }

        List<Student> studentList = studentService.conditionSearch(studentNumber, name, classes);
        for(Student stu:studentList){
            Role role = new Role();
            role.setROLE_id(2);
            role.setROLE_name("ROLE_student");
            stu.setRole(role);
        }
        if (0 != studentList.size()) {
            HashMap hashMap = new HashMap();
            //返回值的key必须为 'content'  (layUI)
            hashMap.put("content", studentList);
            System.out.println(studentList);
            return RestResponses.ok(hashMap);
        } else {
            return RestResponses.fail("暂无数据");
        }
    }

    /**
     * 跳转至学生信息编辑页面
     *
     * @return
     */
    @RequestMapping(value = "/back/toEditStudent/{studentNumber}", method = RequestMethod.GET)
    public String toEditRecruitment(@PathVariable("studentNumber") String studentNumber, Model model) {
        Student student = studentService.queryOne(studentNumber);
        model.addAttribute("student", student);
        return "back/UserManage/studentEdit";
    }

    /**
     * 修改学生信息
     *
     * @param student
     * @return
     */
    @RequestMapping("/back/updateStudent")
    @ResponseBody
    public RestResponses updateProject(Student student) {

        System.out.println("student"+student);
        // 根据学号修改
        int i = studentService.updateStudent(student);
        log.info("修改--" + student.getNum() + "--的信息");
        if (i != 0) {
            return RestResponses.ok();
        } else {
            return RestResponses.fail(200, "学生信息更新失败...");
        }

    }

    /**
     * 删除学生信息
     *
     * @param studentNumber
     * @return
     */
    @RequestMapping("/back/delStudent")
    @ResponseBody
    public RestResponses delStudent(@RequestParam("studentNumber") String studentNumber) {
        int i = studentService.delStudent(studentNumber);
        if (i > 0) {
            log.info("删除成功");
            return RestResponses.ok();
        } else {
            log.info("删除失败");
            return RestResponses.fail();
        }
    }


}
